<?php
/**
 * 网站配置
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/10
 * Time: 下午6:34
 */

namespace app\control\controller;


use app\common\model\WebConfigModel;
use think\facade\Config;
use think\facade\Request;

class ConfController extends AdminBaseController
{
    /**
     * 配置列表
     */
    public function index(){
        $data=Request::param("");
        $where=$this->search($data);

        $wc=new WebConfigModel();
        $list=$wc->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){
        $where[]=["status",">",WebConfigModel::STATUS_DEL];

        if(!empty($data["title"])){
            $where[]=["title","like","%{$data['title']}%"];
        }

        if(!empty($data["name"])){
            $where[]=["name",'like',"%{$data['name']}%"];
        }

        return $where;
    }

    /**
     * 增加/编辑
     * @param int $id
     * @return mixed
     */
    public function edit($id=0){
        if(Request::isPost()){
            $data=Request::param();

            $AM=new WebConfigModel();
            $status=$AM->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=WebConfigModel::get($id);
            $plist=Config::has('WEB_SET_CAT')?Config::get('WEB_SET_CAT'):[];
            $wlist=Config::has('WEB_SET')?Config::get('WEB_SET'):[];

            $this->assign("info",$info);
            $this->assign("plist",$plist);
            $this->assign("wlist",$wlist);
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=WebConfigModel::get($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 排序
     */
    public function acOrder(){
        $id=Request::post("id");
        $orders=Request::post("orders");

        $am=WebConfigModel::get($id);
        $am->list_order=$orders;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 删除分类
     */
    public function del(){
        $id=Request::post("id");

        $AM=new WebConfigModel();
        $status=$AM->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(){
        $id=Request::post("ids");

        $AM=new WebConfigModel();

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$AM->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }
}